/* GCW     06/06/94   */
/* convert a number n to a signed string to base b */

#ifndef _BOBCONSTANTS
#include bob:Constants
#endif

#ifndef _string_string
#define _string_string 1
#endif

/* convert an integer n to a string in base b */
string(n,b)  
{
 local neg,s,d;
 if (n == 0) return ("0");
 if (neg = (n<0)) n = -n;
 s = "";
 while (n)
   {
    d = n%b;
    s = ((d<10)?(d +'0'):(d+'W'))+s;
    n /= b;
   }
 return (((neg)?"-":"")+s);
}

/* convert a number n to an unsigned hexadecimal string */
hex(n)
{
 local s,_byte,top,bottom,b,i,zero;
 s = "&";zero = TRUE;i = 4;
 b = @(newstring(4));
 in b put { n; }
 while (i>0 && zero)
   {
    i--;
    if (_byte=byte(b+i)) zero = FALSE;
   }
 top = _byte/16; bottom = _byte%16;
 if (top) s += (top<10)?(top+'0'):(top+'W');
 s += (bottom<10)?(bottom+'0'):(bottom+'W'); 
 while (i>0)
   {
    i--; _byte = byte(b+i); top = _byte/16; bottom = _byte%16;
    s += (top<10)?(top+'0'):(top+'W');
    s += (bottom<10)?(bottom+'0'):(bottom+'W');
   }
 return s;
} 
